package cn.eonshine.cuba.inf.modular.basic.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.eonshine.base.core.base.controller.BaseController;
import cn.eonshine.cuba.inf.common.http.Message;
import cn.eonshine.cuba.inf.modular.basic.entity.Mapping;
import cn.eonshine.cuba.inf.modular.basic.service.IMappingService;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-07-28 16:24:33
 */
@RestController
@RequestMapping("/mapping")
public class MappingController extends BaseController {

    private String PREFIX = "/basic/mapping/";

    @Autowired
    private IMappingService mappingService;

    @RequestMapping("")
    public String index() {
        return PREFIX + "mapping.html";
    }

    @RequestMapping("/mapping_add")
    public String mappingAdd() {
        return PREFIX + "mapping_add.html";
    }

    @RequestMapping("/mapping_update/{mappingId}")
    public String mappingUpdate(@PathVariable Integer mappingId, Model model) {
        Mapping mapping = mappingService.getById(mappingId);
        model.addAttribute("item",mapping);
        return PREFIX + "mapping_edit.html";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Message list(@RequestBody Message message) {
        return mappingService.queryDataList(message);
    }

    @RequestMapping(value = "/add")
    public Message add(@RequestBody Message message) {
    	Message result=mappingService.insertMapping(message);
        return result;
    }

    @RequestMapping(value = "/delete/{mappingId}")
    @ResponseBody
    public Message delete(@PathVariable("mappingId") String mappingId, @RequestBody Message message) {
    	Message result=mappingService.deleteOne(mappingId,message);
        return result;
        
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Mapping mapping) {
        mappingService.updateById(mapping);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/detail/{mappingId}")
    @ResponseBody
    public Object detail(@PathVariable("mappingId") String mappingId, @RequestBody Message message) {
        return mappingService.queryDataDetail(mappingId,message);
//        return keyService.serverEncryptData(JSONUtil.toJsonStr(page.getRecords()), page.getRecords().size(),
//				clientMessage.getPubKey());
//        return mappingService.queryDataList(message);
    }
}
